package cn.iocoder.yudao.module.iot.service.phonemsglog;

import cn.iocoder.yudao.module.iot.enums.IotErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.phonemsglog.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.phonemsglog.PhoneMsgLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.iot.dal.mysql.phonemsglog.PhoneMsgLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 短信和电话消息提醒记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PhoneMsgLogServiceImpl implements PhoneMsgLogService {

    @Resource
    private PhoneMsgLogMapper phoneMsgLogMapper;

    @Override
    public Integer createPhoneMsgLog(PhoneMsgLogSaveReqVO createReqVO) {
        // 插入
        PhoneMsgLogDO phoneMsgLog = BeanUtils.toBean(createReqVO, PhoneMsgLogDO.class);
        phoneMsgLogMapper.insert(phoneMsgLog);
        // 返回
        return phoneMsgLog.getId();
    }

    @Override
    public void updatePhoneMsgLog(PhoneMsgLogSaveReqVO updateReqVO) {
        // 校验存在
        validatePhoneMsgLogExists(updateReqVO.getId());
        // 更新
        PhoneMsgLogDO updateObj = BeanUtils.toBean(updateReqVO, PhoneMsgLogDO.class);
        phoneMsgLogMapper.updateById(updateObj);
    }

    @Override
    public void deletePhoneMsgLog(Integer id) {
        // 校验存在
        validatePhoneMsgLogExists(id);
        // 删除
        phoneMsgLogMapper.deleteById(id);
    }

    private void validatePhoneMsgLogExists(Integer id) {
        if (phoneMsgLogMapper.selectById(id) == null) {
            throw exception(IotErrorCodeConstants.PHONE_MSG_LOG_NOT_EXISTS);
        }
    }

    @Override
    public PhoneMsgLogDO getPhoneMsgLog(Integer id) {
        return phoneMsgLogMapper.selectById(id);
    }

    @Override
    public PageResult<PhoneMsgLogDO> getPhoneMsgLogPage(PhoneMsgLogPageReqVO pageReqVO) {
        return phoneMsgLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PhoneMsgLogDO> getSendList(String deviceName) {
        return phoneMsgLogMapper.getSendList(deviceName);
    }

}